//
//  BaseTabBarController.m
//  MyItem
//
//  Created by admin on 15/12/23.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseNavController.h"
@interface BaseTabBarController ()
{
    UIImageView *_bgTabBarView;//TabBar背景
    NSInteger _selectedButtonTag;
    NSArray *_buttonNormal;
    NSArray *_buttonsSelected;
}
@end

@implementation BaseTabBarController


- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化需要加载的数据
    [self _loadData];
    //加载各个模块rootViewController
    [self _LoadController];
    //加载tabBara按钮
    [self _initTabBarButton];
    _selectedButtonTag = 10;
    // Do any additional setup after loading the view.
}
- (void)_loadData{
    _buttonNormal = @[@"tabBar_home_normal@2x",@"tabBar_category_normal@2x",@"tabBar_find_normal@2x",@"tabBar_cart_normal@2x",@"tabBar_myJD_normal@2x"];
    _buttonsSelected = @[@"tabBar_home_press@2x",@"tabBar_category_press@2x",@"tabBar_find_press@2x",@"tabBar_cart_press@2x",@"tabBar_myJD_press@2x"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark  初始化APP
//添加每个模块根视图控制器
- (void)_LoadController{
    //获取StoryBoard 名字
    NSArray *storyboardName = @[@"HomeStoryboard",@"ClassificationViewController",@"FindStoryboard",@"ShoppingCartStoryboard",@"MyCenterStoryboard",];
    NSMutableArray *navCtrl = [[NSMutableArray alloc]initWithCapacity:0];
    for (int i = 0; i < storyboardName.count; i++) {
        //先加载StoryBoard 取得导航控制器；
        BaseNavController *baseNavCtrl = [UIStoryboard storyboardWithName:storyboardName[i] bundle:nil].instantiateInitialViewController;
        [navCtrl addObject:baseNavCtrl];
    }
    self.viewControllers = navCtrl;
}
//初始化tabbar 底部按钮
- (void)_initTabBarButton{
    
    //移除先前tabbar的Button
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:(NSClassFromString(@"UITabBarButton"))]) {
            [view removeFromSuperview];
        }
    }
    _bgTabBarView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, HScreenWidth, 55)];
    _bgTabBarView.image = [UIImage imageNamed:@"blackBack@2x"];
    [self.tabBar addSubview:_bgTabBarView];
    CGFloat buttonWidth = HScreenWidth / 5;
    //for循环创建UIcontrol
    for (int i = 0; i < 5; i++) {
        UIControl *button = [[UIControl alloc]initWithFrame:CGRectMake(i * buttonWidth, 0, HScreenHeight, 49)];
        [button addTarget:self action:@selector(_buttonSelected:) forControlEvents:    UIControlEventTouchUpInside ];
        button.tag = i + 10 ;
        [self.tabBar addSubview:button];
        //创建按钮显示的视图
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((buttonWidth - 62)/2,0,62 , 49)];
        imageView.image = [UIImage imageNamed:_buttonNormal[i]];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.tag = i + 20;
        [button addSubview: imageView];
        //改变初始化状态默认
        if (i == 0) {
            imageView.image = [UIImage imageNamed:_buttonsSelected[0]];
            button.selected = YES;
        }
    }
    _selectedButtonTag = 10;
    
}
- (void)_buttonSelected:(UIButton *)button{
    self.selectedIndex = button.tag - 10 ;
    if (_selectedButtonTag != button.tag) {
        //改变先前选中的button显示
        UIControl *oldButton = [self.tabBar viewWithTag:_selectedButtonTag];
        UIImageView *oldImageView = (UIImageView *)[oldButton viewWithTag:(_selectedButtonTag + 10)];
        oldImageView.image = [UIImage imageNamed:_buttonNormal[ _selectedButtonTag -10]];
        //改变当前选中的button显示
        UIImageView *NewImageView = (UIImageView *)[button viewWithTag:(button.tag + 10)];
        NewImageView.image = [UIImage imageNamed:_buttonsSelected[button.tag -10]];
        _selectedButtonTag = button.tag;
    }
}
@end
